package logic

import (
	"context"
	"errors"
	"shuqi-novel/models/mysql"

	"shuqi-novel/novelrpc/internal/svc"
	"shuqi-novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelAddLogic {
	return &NovelAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 小说添加
func (l *NovelAddLogic) NovelAdd(in *novelrpc.NovelAddRequest) (*novelrpc.NovelAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.FirstUserByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("不是添加用户")
	}
	novel := mysql.Novel{
		Name:      in.Name,
		Title:     in.Title,
		Content:   in.Content,
		Author:    in.Author,
		NovelType: in.NovelType,
		UserId:    in.UserId,
	}
	err = novel.NovelAdd(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("添加失败")
	}

	return &novelrpc.NovelAddResponse{
		NovelId: int64(novel.ID),
	}, nil
}
